<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = useRouter()
const registerFormRef = ref()

const selfUser = ref({
    userAccountName: '',
    userPassword: ''
})

const rules = {
    userAccountName: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { max: 20, message: '用户名至多为20位', trigger: 'blur' }
    ],
    userPassword: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        // { min: 6, max: 18, message: '长度6-18位', trigger: 'blur' }
    ]
}

const handleRegister = async () => {
    try {
        await registerFormRef.value.validate()
        
        console.log('注册数据:', selfUser.value)
        ElMessage.success('注册成功')
        setTimeout(() => router.push({ name: 'topic-recommend' }), 800)
    } catch (error) {
        ElMessage.warning('请正确填写表单')
    }
}

const toLogin = () => {
    router.push({ name: 'login' })
}
</script>

<template>
    <div class="login-container">
        <div class="login-box">
            <div class="login-header">
                <h2>用户注册</h2>
            </div>

            <div class="login-body">
                <el-form ref="registerFormRef" :model="selfUser" :rules="rules" label-width="80px">
                    <el-form-item label="用户名" prop="userAccountName">
                        <el-input v-model="selfUser.userAccountName" placeholder="请输入用户名" clearable maxlength="20" />
                    </el-form-item>

                    <el-form-item label="密码" prop="userPassword">
                        <el-input v-model="selfUser.userPassword" type="password" placeholder="请输入密码" show-password clearable />
                    </el-form-item>

                    <div class="action-buttons">
                        <el-button type="primary" class="login-btn" @click="handleRegister">
                            立即注册
                        </el-button>
                        <el-button class="register-btn" @click="toLogin">
                            返回登录
                        </el-button>
                    </div>
                </el-form>
            </div>
        </div>
    </div>
</template>


<style scoped>
.login-container {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    background: #f0f2f5;
}

.login-box {
    width: 400px;
    padding: 30px;
    background: white;
    border-radius: 8px;
    box-shadow: 0 2px 12px rgba(0,0,0,0.1);
}

.login-header {
    text-align: center;
    margin-bottom: 30px;
}

.login-header h2 {
    color: #333;
    margin-bottom: 20px;
}

.el-form-item {
    margin-bottom: 22px;
}

.el-input {
    width: 85%;
}

.action-buttons {
    display: block;
    margin-top: 40px;
}

.login-btn {
    display: block;
    height: 40px;
    width: 100%;
}

.register-btn {
    display: block;
    margin: 20px auto;
    height: 40px;
    width: 100%;
    background-color: #f5f7fa;
    border-color: #e4e7ed;
    color: #606266;
}

.register-btn:hover {
    background-color: #e4e7ed;
}
</style>